package com.lib.okhttp;

import com.lib.okhttp.annotation.CacheType;
import com.lib.okhttp.annotation.Encoding;

import java.util.HashMap;
import java.util.Map;

import okhttp3.Call;

/**
 * 封装网络请求
 *
 * @author 一叶丶枫凌
 * Created on 2021-10-05 14:26
 */
public class NetRequest {
    public static final int Scheduler_MAIN = 0;
    public static final int Scheduler_IO = 1;
    /**
     * 调度线程
     */
    private int mNetScheduler;
    /**
     * 请求类型
     */
    private String mCacheType;
    /**
     * 编码方式
     */
    private String mRequestEncoding;
    /**
     * 请求地址
     */
    private String mUrl;
    /**
     * 加密的密钥
     */
    private String mSecretKey;
    /**
     * 请求头参数
     */
    private Map<String, String> mHeadMap;
    /**
     * 请求体：Byte
     */
    private byte[] mParamBytes;
    /**
     * 请求体：Json
     */
    private String mParamJson;
    /**
     * 请求体：Form表单
     */
    private HashMap<String, String> mFormBodyMap;

    private Get mGet;
    private Post mPost;

    private NetRequest() {
        mNetScheduler = Scheduler_MAIN;
        mCacheType = CacheType.AUTO;
        mRequestEncoding = Encoding.UTF_8;
//        mSecretKey = "SecretKey_20151121!@#";
    }

    public NetRequest observeOn(int netScheduler) {
        mNetScheduler = netScheduler;
        return this;
    }

    public boolean isMain() {
        return mNetScheduler == Scheduler_MAIN;
    }

    public String getCacheType() {
        return mCacheType;
    }

    public NetRequest setCacheType(String cacheType) {
        mCacheType = cacheType;
        return this;
    }

    public String getUrl() {
        return mUrl;
    }

    public NetRequest setUrl(String url) {
        mUrl = url;
        return this;
    }

    public NetRequest setRequestEncoding(String requestEncoding) {
        mRequestEncoding = requestEncoding;
        return this;
    }

    public String getRequestEncoding() {
        return mRequestEncoding.toLowerCase();
    }

    public NetRequest setSecretKey(String secretKey) {
        mSecretKey = secretKey;
        return this;
    }

    public String getSecretKey() {
        return mSecretKey;
    }

    public Get doGet() {
        if (mGet == null) {
            mGet = new Get();
        }
        return mGet;
    }

    public Post doPost() {
        if (mPost == null) {
            mPost = new Post();
        }
        return mPost;
    }

    public static NetRequest newInstance() {
        return new NetRequest();
    }

    public class Get {

        //<editor-fold desc="表单数据">
        public Get setParamFormBody(HashMap<String, String> formBodyMap) {
            if (mFormBodyMap == null) {
                mFormBodyMap = new HashMap<>();
            }
            mFormBodyMap.putAll(formBodyMap);
            return this;
        }

        public Get addParamFormBody(String k, String v) {
            if (mFormBodyMap == null) {
                mFormBodyMap = new HashMap<>();
            }
            mFormBodyMap.put(k, v);
            return this;
        }

        public HashMap<String, String> getParamFormBody() {
            return mFormBodyMap;
        }
        //</editor-fold>

        public NetRequest build() {
            return NetRequest.this;
        }

        public Call doReq(NetClient netManager, Class<?> clazz) {
            return netManager.doGet(NetRequest.this, clazz);
        }
    }

    public class Post {

        //<editor-fold desc="请求头参数">
        public Post addHead(String k, String v) {
            if (mHeadMap == null) {
                mHeadMap = new HashMap<>();
            }
            mHeadMap.put(k, v);
            return this;
        }

        public Map<String, String> getHeadMap() {
            return mHeadMap;
        }
        //</editor-fold>

        //<editor-fold desc="请求体参数">
        public Post setParamBytes(byte[] paramBytes) {
            mParamBytes = paramBytes;
            return this;
        }

        public byte[] getParamBytes() {
            return mParamBytes;
        }

        public Post setParamJson(String paramJson) {
//        if (!TextUtils.isEmpty(mSecretKey)) {
//            paramJson = XXTEA.encryptStr(paramJson, mSecretKey);
//        }
            mParamJson = paramJson;
            return this;
        }

        public String getParamJson() {
            return mParamJson;
        }

        public Post setParamFormBody(HashMap<String, String> formBodyMap) {
            if (mFormBodyMap == null) {
                mFormBodyMap = new HashMap<>();
            }
            mFormBodyMap.putAll(formBodyMap);
            return this;
        }

        public Post addParamFormBody(String k, String v) {
            if (mFormBodyMap == null) {
                mFormBodyMap = new HashMap<>();
            }
            mFormBodyMap.put(k, v);
            return this;
        }

        public HashMap<String, String> getParamFormBody() {
            return mFormBodyMap;
        }

        //</editor-fold>
        public NetRequest build() {
            return NetRequest.this;
        }

        public Call doReq(NetClient netManager, Class<?> clazz) {
            return netManager.doPost(NetRequest.this, clazz);
        }
    }
}